from threading import Lock
import json


class JSONHelper(object):

    def __init__(self, filename):
        self.file = open(filename, 'w+')
        self.lock = Lock()

    def write(self, obj):
        try:
            self.lock.acquire()
            obj_str = json.dumps(obj, indent=4)
            self.file.write(obj_str)
        finally:
            self.lock.release()

    def close(self):
        self.file.close()